<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="br.com.voefacil.modelo.Voo"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Site de LPBD">
        <meta name="author" content="Daniel Lucas">
        <link href="resources/css/bootstrap.css" rel="stylesheet">
        <link href="resources/css/bootstrap-responsive.css" rel="stylesheet">
        <link href="resources/css/datepicker.css" rel="stylesheet">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Passagens</title>
    </head>
    <body>
        <script>
            var id = null;
            var voo = null;
            var destino = null;
            var data = null;
            var classe = null;
            var hora = null;
            var valor = null;
            var adultos = null;
            var bebes = null;
            var criancas = null;
            
            function setId(nid, nvoo, ndestino, ndata, nclasse, nhora, nvalor, nadultos, nbebes, ncriancas){
                id  =  nid;
                voo = nvoo;
                destino = ndestino;
                data = ndata;
                classe = nclasse;
                hora = nhora;
                adultos = nadultos;
                bebes = nbebes;
                criancas = ncriancas;
                valor = nvalor;
                //alert("id:"+id+" voo:"+voo+" destino:"+destino+" data:"+ndata+" classe:"+classe+" hora:"+hora+" adultos:"+adultos+" bebes:"+bebes+" criancasgays:"+criancas+" valor:"+valor);
            }
            function calcPass(vl, adult, crianc, beb){
                total = vl;
                adultos = adult;
                criancas = (crianc/2)+1;
                bebes = (beb/6)+1;
                total = total* bebes * adultos * criancas;
                return total;
            }
            function ir(){
                var res = calcPass(valor, adultos, criancas, bebes);
                window.location.href="Passagem.jsp?id="+id+"&voo="+voo+"&destino="+destino+"&data="+data+"&classe="+classe+"&hora="+hora+"&valor="+res;
            }
        </script>
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container-fluid">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="#"> VoeFácil 1.0</a>

                    <div class="nav-collapse">
                        <ul class="nav">
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="empresa.jsp">Quem Somos</a></li>
                            <li><a href="cancela.jsp">Cancelar Passagem</a></li>
                            <li><a href="contato.jsp">Contato</a></li>
                            <li><a href="Criadores.jsp">Desenvolvedores</a></li>
                        </ul>
                    </div><!--/.nav-collapse -->
                </div>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row">
                <div class="span3">
                    <div class="well sidebar-nav">
                        <ul class="nav nav-list">
                            <li class="nav-header">Menu</li>
                            <li class="nav-header">Menu</li>
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="empresa.jsp">Quem Somos</a></li>
                            <li><a href="cancela.jsp">Cancelar Passagem</a></li>
                            <li><a href="contato.jsp">Contato</a></li>
                        </ul>
                    </div><!--/.well -->
                </div><!--/span-->
                <%Connection cn = null;
                            String erro = "";
                            List<Voo> lista = new ArrayList<Voo>();
                            int adultos = 0;
                            int criancas = 0;
                            int bebes = 0;
                            try {
                                String origem = request.getParameter("origem");
                                String data = request.getParameter("data");
                                String destino = request.getParameter("destino");
                                String servico = request.getParameter("servico");
                                adultos = Integer.parseInt(request.getParameter("adultos"));
                                criancas = Integer.parseInt(request.getParameter("criancas"));
                                bebes = Integer.parseInt(request.getParameter("bebes"));
                                Class.forName("oracle.jdbc.driver.OracleDriver");
                                cn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "123");
                                Statement stm = cn.createStatement();
                                ResultSet rs = stm.executeQuery("SELECT cd_passagem, voo, nm_origem, dt_partida, hr_partida,  nm_destino, vl_passagem, classe FROM passagem WHERE status='Disponivel' and nm_origem = '" + origem + "' and dt_partida='" + data + "' and nm_destino = '" + destino + "' and classe='" + servico + "'");
                                while (rs.next()) {
                                    Voo v = new Voo(rs.getString("voo"), rs.getString("dt_partida"), rs.getString("hr_partida"), rs.getString("nm_destino"), rs.getDouble("vl_passagem"), rs.getString("classe"), Integer.parseInt(rs.getString("cd_passagem")));
                                    lista.add(v);
                                }
                                rs.close();
                            } catch (Exception ex) {
                                erro = ex.getMessage();
                            }
                %>
                <script>
                    adultos = "<%= adultos%>";
                    criancas = "<%= criancas%>";
                    bebes = "<%= bebes%>";
                </script>
                <fieldset class="well" style="width: 880px; background-color: azure" >
                    <legend>Passagens</legend>
                    <div class="row" style="padding-left: 50px; padding-top: 7px" >
                        <div class="span8" align="center">
                            <table class="table table-striped">
                                <thead>
                                    <tr>PASSAGENS</tr>
                                    <tr>
                                        <th>Voo</th>
                                        <th>Destino</th>
                                        <th>Data</th>
                                        <th>Hora</th>
                                        <th>Classe</th>
                                        <th>Preço</th>
                                        <th>Comprar</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <% for (int i = 0; i < lista.size(); i++) {
                                    %>
                                    <tr>
                                        <td><%= lista.get(i).getVoo()%></td>
                                        <td><%= lista.get(i).getDestino()%></td>
                                        <td><%= lista.get(i).getData().toString().subSequence(0, 10).toString().replace("-", "/")%></td>
                                        <td><%= lista.get(i).getHora().substring(9, 18).toString().replaceFirst(" ", "").toString().replaceFirst(".", "")%></td>
                                        <td><%= lista.get(i).getClasse()%></td>
                                        <td><%= lista.get(i).getValor()%></td>
                                        <td><input name="id" value="<%= lista.get(i).getCodigo()%>" type="radio"
                                                   onclick="javascript:setId('<%= lista.get(i).getCodigo()%>','<%= lista.get(i).getVoo()%>','<%= lista.get(i).getDestino()%>','<%= lista.get(i).getData()%>','<%= lista.get(i).getClasse()%>','<%= lista.get(i).getHora()%>','<%= lista.get(i).getValor()%>', adultos, criancas, bebes)"/></td>
                                    </tr>
                                    <% }%>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <!-- -->
                    <center>
                        <div class="control-group">
                            <label class="control-label" for="input01"></label>
                            <div class="controls">
                                <a class="btn btn-success right" onclick="javascript:ir()">Comprar</a>
                                <input type="reset" class="btn btn-danger right" value="Limpar"/>
                            </div>
                        </div>
                    </center>
                </fieldset>
            </div>
        </div>
        <hr>
    <footer>
        <div class="container">
            <p>&copy; VoeFacil</p>
        </div>
    </footer>
</body>
</html>